import Vue from 'vue'
import { Toast } from 'vant';
import Router from 'vue-router'
//import Home from '../views/home.vue'
//import User from '../views/User.vue'
import NewsDetail from '../views/NewsDetail.vue'
//import UserAdd from '../views/UserAdd.vue'
//import Login from '../views/Login.vue'
//import Mine from '../views/Mine.vue'
Vue.use(Router)
const router= new Router({
  routes: [
    {
      path:'/',
      name:'Login',
      component:()=>import('../views/Login.vue')
    },
    {
      path: '/home',
      name: 'Home',
      component: ()=>import('../views/home.vue')
    },
    {
      path:'/user',
      name:'User',
      component:()=>import('../views/User.vue')
    },
    {
      path:'/useradd',
      name:'UserAdd',
      component:()=>import('../views/UserAdd.vue')
    },
    {
      path:'/newsdetail/:id',
      component:NewsDetail
    },
    {
      path:'/mine',
      component:()=>import('../views/Mine.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  const username=window.sessionStorage.getItem('username')
  if (to.name !== 'Login' && !username){
    Toast('登录信息已过期，请重新登录');
    next({ name: 'Login' })
  }
  else next()
})
export default router